package com.uphone.magicleafteacher.pro.activity.listenertraining;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.SizeUtils;
import com.google.gson.Gson;
import com.radish.baselibrary.Intent.IntentData;
import com.radish.baselibrary.Intent.IntentUtils;
import com.radish.baselibrary.navigationbar.DefaultNavigationBar;
import com.uphone.magicleafteacher.MyApp;
import com.uphone.magicleafteacher.R;
import com.uphone.magicleafteacher.base.mvp.BaseMvpActivity;
import com.uphone.magicleafteacher.http.ApiService;
import com.uphone.magicleafteacher.http.RxSchedulers;
import com.uphone.magicleafteacher.pro.activity.listenertrainscore.ListenerTrainScoreActivity;
import com.uphone.magicleafteacher.pro.adapter.PracticeAdapter;
import com.uphone.magicleafteacher.util.AudioPlayProgressUtil;
import com.uphone.magicleafteacher.util.CommonUtil;
import com.uphone.magicleafteacher.util.DialogUtils;
import com.uphone.magicleafteacher.util.GsonUtils;
import com.uphone.magicleafteacher.vo.AudioWorkDetailBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Time: 2019/10/29
 */
public class ListenerTrainingActivity extends BaseMvpActivity<ListenerTrainingPresenter> implements ListenerTrainingContract.View {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.iv_play)
    ImageView ivPlay;
    @BindView(R.id.img_close)
    ImageView imgClose;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.btn_unpass)
    Button btnUnpass;
    @BindView(R.id.btn_pass)
    Button btnPass;
    @IntentData
    private String id;
    @IntentData
    private String title = "";

    int mIndex = 0;
    private DefaultNavigationBar navigationBar;
    private AudioPlayProgressUtil audioUtil;
    private PracticeAdapter mAdapter;
    private SharedPreferences preferences;
    private String token;
    private ArrayList<AudioWorkDetailBean> beanList;
    private AudioWorkDetailBean workDetailBean;

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void initBundle() {

    }

    @Override
    protected int initLayout() {
        return R.layout.activity_retelling_learn_detail;
    }

    @Override
    protected void initView() {
        Intent intent = getIntent();
        preferences = getSharedPreferences("moye", MODE_PRIVATE);
        token = preferences.getString("token", "");
        audioUtil = new AudioPlayProgressUtil();
        audioUtil.setPlayView(ivPlay);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));


    }


    @Override
    protected void initTitle() {
        tvTitle.setText(CommonUtil.getStr(title));
    }

    private void setRightNavigationText() {
        SpannableString textSpanned1 = new SpannableString(mIndex + "/" + mAdapter.getData().size());
        textSpanned1.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.themecolor)),
                0, mIndex + "".length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        textSpanned1.setSpan(new AbsoluteSizeSpan(SizeUtils.sp2px(15)),
                0, mIndex + "".length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        textSpanned1.setSpan(new AbsoluteSizeSpan(SizeUtils.sp2px(11)),
                (mIndex + "").length(), textSpanned1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        navigationBar.setRightTitle(textSpanned1);
    }

    @Override
    protected void initData() {
        ;
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        Map<String, Object> map1 = new HashMap<>();
        map1.put("workId", id);
        MyApp.apiService(ApiService.class)
                .workDetail(map, map1)
                .compose(RxSchedulers.io_main())
                .subscribe(bean -> {
                    if (ListenerTrainingActivity.this != null)
                        ListenerTrainingActivity.this.closeLoading();
                    if (bean != null) {
                        try {
                            String jsonStr = bean.string();
                            Gson gson = GsonUtils.getGson();
                            workDetailBean = gson.fromJson(jsonStr, AudioWorkDetailBean.class);
                            mAdapter = new PracticeAdapter(workDetailBean.getAudioWork().getChapters());
                            recyclerView.setAdapter(mAdapter);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    @Override
    protected void initListener() {
    }

    @OnClick({R.id.iv_play, R.id.btn_unpass, R.id.btn_pass, R.id.img_close})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_close:
                finish();
                break;
            case R.id.iv_play:
                if (!view.isSelected() && workDetailBean != null) {
                    audioUtil.play(CommonUtil.getVideoUrl(workDetailBean.getAudioWork().getAudioPath()));
                } else {
                    audioUtil.stop();
                }
                break;
            case R.id.btn_unpass:
                audioUtil.stop();
                DialogUtils.alterDialog(this, "学生即将重新练习并且提交测试，确定该学生语音不通过吗？", (dialog, view1, type) -> {
                    mPresenter.authAudioWork(id, 2);
                });
                break;
            //通过
            case R.id.btn_pass:
                audioUtil.stop();
                mPresenter.authAudioWork(id, 1);
                break;
        }
    }


    @OnClick()
    public void onViewClicked() {
    }

    @Override
    public void authAudioWork(int auth) {
        if (auth == 1) {//通过
            IntentUtils.getInstance().with(this, ListenerTrainScoreActivity.class)
                    .putString("title", title)
                    .putString("id", id)
                    .start();

        } else if (auth == 2) {//不通过
            CommonUtil.quitActivity(ListenerTrainingActivity.this, "语音已驳回");
        }
    }
}
